package com.uzai.console.service.profit.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.gson.internal.$Gson$Preconditions;
import com.uzai.common.exception.BusinessException;
import com.uzai.common.result.ResponseCode;
import com.uzai.console.common.utils.DateUtil;
import com.uzai.console.common.utils.Tools;
import com.uzai.console.dto.profit.normalrewardrule.NormalRewardRuleQuery;
import com.uzai.console.dto.profit.nuserrewardrule.NuserRewardRuleIdDto;
import com.uzai.console.dto.profit.nuserrewardrule.NuserRewardRuleInfoDto;
import com.uzai.console.dto.profit.nuserrewardrule.NuserRewardRuleQuery;
import com.uzai.console.dto.profit.nuserrewardrulegroup.NuserRewardRuleGroupIdDto;
import com.uzai.console.dto.profit.nuserrewardrulegroup.NuserRewardRuleGroupInfoDto;
import com.uzai.console.dto.profit.nuserrewardrulegroup.NuserRewardRuleGroupQuery;
import com.uzai.console.dto.sysplugin.SysPluginQuery;
import com.uzai.console.entity.*;
import com.uzai.console.mapper.*;
import com.uzai.console.service.profit.NuserRewardRuleService;
import com.uzai.console.vo.profit.nuserrewardrule.NuserRewardRuleInfoVo;
import com.uzai.console.vo.profit.nuserrewardrulegroup.NuserRewardRuleGroupInfoVo;
import com.uzai.console.vo.sysplugin.SysPluginVo;
import org.apache.catalina.util.ToStringUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.builder.ToStringSummary;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/***
 * @author liuqi
 * @date 2021年09月01日 19:26
 */
@Service
public class NuserRewardRuleServiceImpl implements NuserRewardRuleService {

    @Autowired
    private NuserRewardRuleMapper nuserRewardRuleMapper;
    @Autowired
    private NuserRewardRuleGroupMapper nuserRewardRuleGroupMapper;
    @Autowired
    private SysPluginMapper sysPluginMapper;
    @Autowired
    private LadderRatioGroupMapper ladderRatioGroupMapper;
    @Autowired
    private NormalRewardRuleMapper normalRewardRuleMapper;

    /**
     * 分页查询杀熟规则列表
     * @author liuqi
     * @date 2021/8/25 11:38
     * @param query
     * @return null
     */
    public Page<NuserRewardRuleInfoVo> findNuserRewardRuleList(NuserRewardRuleQuery query){
        //新建返回到页面的LIST对象
        List<NuserRewardRuleInfoVo> nuserRewardRuleInfoVoList = new ArrayList<>();

        //默认按照杀熟规则组排序
        query.setOrderByField("groupId");

        //执行分页查询对象
        Page<NuserRewardRule> page = new Page<>();
        query.convert(page);
        List<NuserRewardRule> nuserRewardRuleList = nuserRewardRuleMapper.findByList(page, query);

        if(nuserRewardRuleList != null){
            for (NuserRewardRule nuserRewardRule : nuserRewardRuleList){
                NuserRewardRuleInfoVo nuserRewardRuleInfoVo = new NuserRewardRuleInfoVo();
                BeanUtils.copyProperties(nuserRewardRule, nuserRewardRuleInfoVo);
                //杀熟规则组名称
                NuserRewardRuleGroup nuserRewardRuleGroup = nuserRewardRuleGroupMapper.selectById(nuserRewardRule.getGroupId(), query.getMerId());
                if(nuserRewardRuleGroup != null){
                    nuserRewardRuleInfoVo.setGroupName(nuserRewardRuleGroup.getName());
                }
                //阶梯规则组名称
                LadderRatioGroup ladderRatioGroup = ladderRatioGroupMapper.selectById(nuserRewardRule.getLadderRatioGroupId(), query.getMerId());
                if(ladderRatioGroup != null){
                    nuserRewardRuleInfoVo.setLadderRatioGroupName(ladderRatioGroup.getName());
                }
                //注册时间小于秒数
                nuserRewardRuleInfoVo.setRegDay(Tools.getInteger(nuserRewardRule.getRegTime()) / 24 /3600); //天数转成秒数
                nuserRewardRuleInfoVoList.add(nuserRewardRuleInfoVo);
            }
        }

        //返回到页面分页对象
        Page<NuserRewardRuleInfoVo> pageVo = new Page<>();
        BeanUtils.copyProperties(page, pageVo);
        pageVo.setRecords(nuserRewardRuleInfoVoList);
        //将分页信息复制到返回到页面的分页MO当中
        return pageVo;
    }

    /**
     * 查询杀熟规则详情
     * @author liuqi
     * @date 2021/8/25 11:38
     * @param nuserRewardRuleIdDto
     * @return null
     */
    public NuserRewardRuleInfoVo findNuserRewardRuleInfo(NuserRewardRuleIdDto nuserRewardRuleIdDto){
        if(nuserRewardRuleIdDto.getId() == null ){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "id不能为空");
        }

        NuserRewardRule nuserRewardRule = nuserRewardRuleMapper.selectById(nuserRewardRuleIdDto.getId(), nuserRewardRuleIdDto.getMerId());

        if(nuserRewardRule == null){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "该杀熟规则不存在");
        }

        NuserRewardRuleInfoVo nuserRewardRuleInfoVo =  new NuserRewardRuleInfoVo();

        BeanUtils.copyProperties(nuserRewardRule, nuserRewardRuleInfoVo);

        //注册时间小于秒数
        nuserRewardRuleInfoVo.setRegDay(Tools.getInteger(nuserRewardRule.getRegTime()) / 24 /3600); //天数转成秒数

        return nuserRewardRuleInfoVo;
    }

    /**
     * 添加杀熟规则
     * @author liuqi
     * @date 2021/8/25 11:38
     * @param nuserRewardRuleInfoDto
     * @return null
     */
    public void addNuserRewardRule(NuserRewardRuleInfoDto nuserRewardRuleInfoDto){
        if(nuserRewardRuleInfoDto.getGroupId() == null ){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请选择杀熟分组");
        }

        NuserRewardRuleGroup nuserRewardRuleGroup = nuserRewardRuleGroupMapper.selectById(nuserRewardRuleInfoDto.getGroupId(), nuserRewardRuleInfoDto.getMerId());
        if(nuserRewardRuleGroup == null ){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "该杀熟分组不存在");
        }

        if(StringUtils.isBlank(nuserRewardRuleGroup.getRuleInfo())){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "该杀熟分组还未配置杀熟规则，请配置该分组的杀熟规则");
        }


        List<Integer> ruleInfoList = null;
        try {
            ruleInfoList = JSONObject.parseArray(nuserRewardRuleGroup.getRuleInfo(), Integer.class);
        }catch (Exception e){

        }
        if(ruleInfoList == null || ruleInfoList.size() == 0){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "该杀熟分组配置的杀熟规则格式不正确，请配置保持该分组的杀熟规则");
        }

        if(nuserRewardRuleInfoDto.getNuserModel() == null) {//杀熟条件模式不能为空
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入杀熟条件模式");
        }

        //如果该杀熟分组的杀熟规则列表不包括此杀熟条件，则提示错误
        if(!ruleInfoList.contains(nuserRewardRuleInfoDto.getNuserModel())){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "该分组配置的杀熟规则没有包括此杀熟条件项");
        }

        //旧版的数据编辑（兼容）
        if(StringUtils.isBlank(nuserRewardRuleGroup.getRuleInfo())){
            if(nuserRewardRuleInfoDto.getNuserModel().intValue() == 0){//按最后下单时间算
                if(nuserRewardRuleInfoDto.getLastOrderDay() == null){
                    throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入最后下单时间大于天数");
                }

                //验证最后下单时间是否重复
                NuserRewardRule nuserRewardRule_lastOrderDay = nuserRewardRuleMapper.selectByLastOrderDay(nuserRewardRuleInfoDto);
                if(nuserRewardRule_lastOrderDay != null){
                    throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "该分组下设置的最后下单时间存在重复");
                }

            }else if(nuserRewardRuleInfoDto.getNuserModel().intValue() == 3){//按注册时间算
                if(nuserRewardRuleInfoDto.getRegDay() == null){
                    throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入注册时间小于天数");
                }
                //设置注册时间秒数
                nuserRewardRuleInfoDto.setRegTime(Tools.getInteger(nuserRewardRuleInfoDto.getRegDay() * 24 *3600));
                //验证佣金范围是否与数据中存在交叉
                NuserRewardRule nuserRewardRule_lastOrderDay = nuserRewardRuleMapper.selectByRegTime(nuserRewardRuleInfoDto);
                if(nuserRewardRule_lastOrderDay != null){
                    throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "该分组下设置的注册时间小于天数存在重复");
                }
            }else{//按有效订单数量算
                if(nuserRewardRuleInfoDto.getMinOrderCount() == null){
                    throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入订单大于等于数");
                }
                if(nuserRewardRuleInfoDto.getMaxOrderCount() == null){
                    throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入订单小于数");
                }

                //最大佣金必须大于等于最小佣金
                if(nuserRewardRuleInfoDto.getMaxOrderCount().intValue() <= nuserRewardRuleInfoDto.getMinOrderCount().intValue()){
                    throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "最大订单数必须超过最小订单数");
                }

                //验证佣金范围是否与数据中存在交叉
                NuserRewardRule nuserRewardRule_orderCount = nuserRewardRuleMapper.selectByOrderCount(nuserRewardRuleInfoDto);
                if(nuserRewardRule_orderCount != null){
                    throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "该分组下设置的有效订单数范围存在重复");
                }
            }
        }else{ //新版数据
            if(nuserRewardRuleInfoDto.getNuserModel().intValue() == 104){//按最后下单时间算
                if(nuserRewardRuleInfoDto.getLastOrderDay() == null){
                    throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入最后下单时间大于天数");
                }

                //验证最后下单时间是否重复
                NuserRewardRule nuserRewardRule_lastOrderDay = nuserRewardRuleMapper.selectByLastOrderDay(nuserRewardRuleInfoDto);
                if(nuserRewardRule_lastOrderDay != null){
                    throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "该分组下设置的最后下单时间存在重复");
                }
            }else if(nuserRewardRuleInfoDto.getNuserModel().intValue() == 105){//按注册时间算
                if(nuserRewardRuleInfoDto.getRegDay() == null){
                    throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入注册时间小于天数");
                }
                //设置注册时间秒数
                nuserRewardRuleInfoDto.setRegTime(Tools.getInteger(nuserRewardRuleInfoDto.getRegDay() * 24 *3600));
                //验证佣金范围是否与数据中存在交叉
                NuserRewardRule nuserRewardRule_lastOrderDay = nuserRewardRuleMapper.selectByRegTime(nuserRewardRuleInfoDto);
                if(nuserRewardRule_lastOrderDay != null){
                    throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "该分组下设置的注册时间小于天数存在重复");
                }
            }else if(nuserRewardRuleInfoDto.getNuserModel().intValue() == 100 || nuserRewardRuleInfoDto.getNuserModel().intValue() == 101){ //全平台
                //全平台订单数
                if(nuserRewardRuleInfoDto.getAllMinOrderCount() == null){
                    throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入全平台订单大于等于数");
                }
                if(nuserRewardRuleInfoDto.getAllMaxOrderCount() == null){
                    throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入全平台订单小于数");
                }
                //最大佣金必须大于等于最小佣金
                if(nuserRewardRuleInfoDto.getAllMaxOrderCount().intValue() <= nuserRewardRuleInfoDto.getAllMinOrderCount().intValue()){
                    throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "最大订单数必须超过最小订单数");
                }
                //验证佣金范围是否与数据中存在交叉
                NuserRewardRule nuserRewardRule_AllOrderCount = nuserRewardRuleMapper.selectByAllOrderCount(nuserRewardRuleInfoDto);
                if(nuserRewardRule_AllOrderCount != null){
                    throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "该分组下设置的全平台订单数范围存在重复");
                }
            }else if(nuserRewardRuleInfoDto.getNuserModel().intValue() == 102 || nuserRewardRuleInfoDto.getNuserModel().intValue() == 103){//各平台
                //淘宝订单数
                if(nuserRewardRuleInfoDto.getTbMinOrderCount() != null && nuserRewardRuleInfoDto.getTbMaxOrderCount() != null){
                    //最大佣金必须大于等于最小佣金
                    if(nuserRewardRuleInfoDto.getTbMaxOrderCount().intValue() <= nuserRewardRuleInfoDto.getTbMinOrderCount().intValue()){
                        throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "淘宝最大订单数必须超过最小订单数");
                    }
                    //验证佣金范围是否与数据中存在交叉
                    NuserRewardRule nuserRewardRule_TbOrderCount = nuserRewardRuleMapper.selectByTbOrderCount(nuserRewardRuleInfoDto);
                    if(nuserRewardRule_TbOrderCount != null){
                        throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "该分组下设置的淘宝订单数范围存在重复");
                    }
                }
                //京东订单数
                if(nuserRewardRuleInfoDto.getJdMinOrderCount() != null && nuserRewardRuleInfoDto.getJdMaxOrderCount() != null){
                    //最大佣金必须大于等于最小佣金
                    if(nuserRewardRuleInfoDto.getJdMaxOrderCount().intValue() <= nuserRewardRuleInfoDto.getJdMinOrderCount().intValue()){
                        throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "京东最大订单数必须超过最小订单数");
                    }
                    //验证佣金范围是否与数据中存在交叉
                    NuserRewardRule nuserRewardRule_JdOrderCount = nuserRewardRuleMapper.selectByJdOrderCount(nuserRewardRuleInfoDto);
                    if(nuserRewardRule_JdOrderCount != null){
                        throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "该分组下设置的京东订单数范围存在重复");
                    }
                }

                //拼多多订单数
                if(nuserRewardRuleInfoDto.getPddMinOrderCount()  != null && nuserRewardRuleInfoDto.getPddMaxOrderCount() != null){
                    //最大佣金必须大于等于最小佣金
                    if(nuserRewardRuleInfoDto.getPddMaxOrderCount().intValue() <= nuserRewardRuleInfoDto.getPddMinOrderCount().intValue()){
                        throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "拼多多最大订单数必须超过最小订单数");
                    }
                    //验证佣金范围是否与数据中存在交叉
                    NuserRewardRule nuserRewardRule_PddOrderCount = nuserRewardRuleMapper.selectByPddOrderCount(nuserRewardRuleInfoDto);
                    if(nuserRewardRule_PddOrderCount != null){
                        throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "该分组下设置的拼多多订单数范围存在重复");
                    }
                }

                //唯品会订单数
                if(nuserRewardRuleInfoDto.getVphMinOrderCount() != null && nuserRewardRuleInfoDto.getVphMaxOrderCount() != null){
                    //最大佣金必须大于等于最小佣金
                    if(nuserRewardRuleInfoDto.getVphMaxOrderCount().intValue() <= nuserRewardRuleInfoDto.getVphMinOrderCount().intValue()){
                        throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "唯品会最大订单数必须超过最小订单数");
                    }
                    //验证佣金范围是否与数据中存在交叉
                    NuserRewardRule nuserRewardRule_VphOrderCount = nuserRewardRuleMapper.selectByVphOrderCount(nuserRewardRuleInfoDto);
                    if(nuserRewardRule_VphOrderCount != null){
                        throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "该分组下设置的唯品会订单数范围存在重复");
                    }
                }

                //抖音订单数
                if(nuserRewardRuleInfoDto.getDyMinOrderCount() != null && nuserRewardRuleInfoDto.getDyMaxOrderCount() == null){
                    //最大佣金必须大于等于最小佣金
                    if(nuserRewardRuleInfoDto.getDyMaxOrderCount().intValue() <= nuserRewardRuleInfoDto.getDyMinOrderCount().intValue()){
                        throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "抖音最大订单数必须超过最小订单数");
                    }
                    //验证佣金范围是否与数据中存在交叉
                    NuserRewardRule nuserRewardRule_DyOrderCount = nuserRewardRuleMapper.selectByDyOrderCount(nuserRewardRuleInfoDto);
                    if(nuserRewardRule_DyOrderCount != null){
                        throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "该分组下设置的抖音订单数范围存在重复");
                    }
                }

                //快手订单数
                if(nuserRewardRuleInfoDto.getKsMinOrderCount() != null && nuserRewardRuleInfoDto.getKsMaxOrderCount() != null){
                    //最大佣金必须大于等于最小佣金
                    if(nuserRewardRuleInfoDto.getKsMaxOrderCount().intValue() <= nuserRewardRuleInfoDto.getKsMinOrderCount().intValue()){
                        throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "快手最大订单数必须超过最小订单数");
                    }
                    //验证佣金范围是否与数据中存在交叉
                    NuserRewardRule nuserRewardRule_KsOrderCount = nuserRewardRuleMapper.selectByKsOrderCount(nuserRewardRuleInfoDto);
                    if(nuserRewardRule_KsOrderCount != null){
                        throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "该分组下设置的快手订单数范围存在重复");
                    }
                }

                //本地生活订单数
                if(nuserRewardRuleInfoDto.getLocalliveMinOrderCount() != null && nuserRewardRuleInfoDto.getLocalliveMaxOrderCount() != null){
                    //最大佣金必须大于等于最小佣金
                    if(nuserRewardRuleInfoDto.getLocalliveMaxOrderCount().intValue() <= nuserRewardRuleInfoDto.getLocalliveMinOrderCount().intValue()){
                        throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "本地生活最大订单数必须超过最小订单数");
                    }
                    //验证佣金范围是否与数据中存在交叉
                    NuserRewardRule nuserRewardRule_LocalliveOrderCount = nuserRewardRuleMapper.selectByLocalliveOrderCount(nuserRewardRuleInfoDto);
                    if(nuserRewardRule_LocalliveOrderCount != null){
                        throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "该分组下设置的本地生活订单数范围存在重复");
                    }
                }
            }
        }

        if(nuserRewardRuleInfoDto.getRewardType() == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请分则分佣方式");
        }

        //总佣金分佣
        if(nuserRewardRuleInfoDto.getRewardType().intValue() == 0){
            if(nuserRewardRuleInfoDto.getUserRatio() == null){
                throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入买家自购比例");
            }
            if(nuserRewardRuleInfoDto.getParentRatio() == null){
                throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入给上级返利比例");
            }
            if(nuserRewardRuleInfoDto.getSlParentRatio() == null){
                throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入给上上级返利比例");
            }
            if(nuserRewardRuleInfoDto.getAgentRatio() == null){
                throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入给代理返利比例");
            }
            if(nuserRewardRuleInfoDto.getSlAgentRatio() == null){
                throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入给上级代理返利比例");
            }
            if(nuserRewardRuleInfoDto.getPartnerRatio() == null){
                throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入给合伙人返利比例");
            }
        }else{ //固定金额奖励
            if(nuserRewardRuleInfoDto.getReUserBal() == null){
                throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入买家奖励金额");
            }
            if(nuserRewardRuleInfoDto.getReParentBal() == null){
                throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入给上级奖励金额");
            }
            if(nuserRewardRuleInfoDto.getReSlParentBal() == null){
                throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入给上上级奖励金额");
            }
            if(nuserRewardRuleInfoDto.getReAgentBal() == null){
                throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入给代理奖励金额");
            }
            if(nuserRewardRuleInfoDto.getReSlAgentBal() == null){
                throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入给上级代理奖励金额");
            }
            if(nuserRewardRuleInfoDto.getRePartnerBal() == null){
                throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入给合伙人奖励金额");
            }
        }

        NuserRewardRule nuserRewardRule = new NuserRewardRule();
        BeanUtils.copyProperties(nuserRewardRuleInfoDto, nuserRewardRule);
        Integer now = DateUtil.getNowTime();
        nuserRewardRule.setId(IdWorker.getId());
        //最后下单时间秒数=最后下单时间天数 * 24 * 3600
        nuserRewardRule.setLastOrderTime(Tools.getInteger(nuserRewardRuleInfoDto.getLastOrderDay()) * 24 * 3600);
        nuserRewardRule.setRegTime(Tools.getInteger(nuserRewardRuleInfoDto.getRegTime()));
        nuserRewardRule.setCreateTime(now);
        nuserRewardRule.setUpdateTime(now);
        nuserRewardRuleMapper.insertSelective(nuserRewardRule);
    }

    /**
     * 修改杀熟规则
     * @author liuqi
     * @date 2021/8/25 11:38
     * @param nuserRewardRuleInfoDto
     * @return null
     */
    public void updateNuserRewardRule(NuserRewardRuleInfoDto nuserRewardRuleInfoDto){

        if(nuserRewardRuleInfoDto.getId() == null ){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入id");
        }

        if(nuserRewardRuleInfoDto.getGroupId() == null ){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请选择杀熟分组");
        }
        NuserRewardRuleGroup nuserRewardRuleGroup = nuserRewardRuleGroupMapper.selectById(nuserRewardRuleInfoDto.getGroupId(), nuserRewardRuleInfoDto.getMerId());
        if(nuserRewardRuleGroup == null ){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "该杀熟分组不存在");
        }

        if(StringUtils.isBlank(nuserRewardRuleGroup.getRuleInfo())){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "该杀熟分组还未配置杀熟规则，请配置该分组的杀熟规则");
        }

        List<Integer> ruleInfoList = null;
        try {
            ruleInfoList = JSONObject.parseArray(nuserRewardRuleGroup.getRuleInfo(), Integer.class);
        }catch (Exception e){

        }
        if(ruleInfoList == null || ruleInfoList.size() == 0){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "该杀熟分组配置的杀熟规则格式不正确，请配置保持该分组的杀熟规则");
        }

        if(nuserRewardRuleInfoDto.getNuserModel() == null) {//杀熟条件模式不能为空
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入杀熟条件模式");
        }

        //如果该杀熟分组的杀熟规则列表不包括此杀熟条件，则提示错误
        if(!ruleInfoList.contains(nuserRewardRuleInfoDto.getNuserModel())){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "该分组配置的杀熟规则没有包括此杀熟条件项");
        }

        //旧版的数据编辑（兼容）
        if(StringUtils.isBlank(nuserRewardRuleGroup.getRuleInfo())){
            if(nuserRewardRuleInfoDto.getNuserModel().intValue() == 0){//按最后下单时间算
                if(nuserRewardRuleInfoDto.getLastOrderDay() == null){
                    throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入最后下单时间大于天数");
                }

                //验证最后下单时间是否重复
                NuserRewardRule nuserRewardRule_lastOrderDay = nuserRewardRuleMapper.selectByLastOrderDay(nuserRewardRuleInfoDto);
                if(nuserRewardRule_lastOrderDay != null && nuserRewardRule_lastOrderDay.getId().longValue() != nuserRewardRuleInfoDto.getId().longValue()){
                    throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "该分组下设置的最后下单时间存在重复");
                }

            }else if(nuserRewardRuleInfoDto.getNuserModel().intValue() == 3){//按注册时间算
                if(nuserRewardRuleInfoDto.getRegDay() == null){
                    throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入注册时间小于天数");
                }
                //设置注册时间秒数
                nuserRewardRuleInfoDto.setRegTime(Tools.getInteger(nuserRewardRuleInfoDto.getRegDay() * 24 *3600));
                //验证佣金范围是否与数据中存在交叉
                NuserRewardRule nuserRewardRule_lastOrderDay = nuserRewardRuleMapper.selectByRegTime(nuserRewardRuleInfoDto);
                if(nuserRewardRule_lastOrderDay != null && nuserRewardRule_lastOrderDay.getId().longValue() != nuserRewardRuleInfoDto.getId().longValue()){
                    throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "该分组下设置的注册时间小于天数存在重复");
                }
            }else{//按有效订单数量算
                if(nuserRewardRuleInfoDto.getMinOrderCount() == null){
                    throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入订单大于等于数");
                }
                if(nuserRewardRuleInfoDto.getMaxOrderCount() == null){
                    throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入订单小于数");
                }

                //最大佣金必须大于等于最小佣金
                if(nuserRewardRuleInfoDto.getMaxOrderCount().intValue() <= nuserRewardRuleInfoDto.getMinOrderCount().intValue()){
                    throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "最大订单数必须超过最小订单数");
                }

                //验证佣金范围是否与数据中存在交叉
                NuserRewardRule nuserRewardRule_orderCount = nuserRewardRuleMapper.selectByOrderCount(nuserRewardRuleInfoDto);
                if(nuserRewardRule_orderCount != null && nuserRewardRule_orderCount.getId().longValue() != nuserRewardRuleInfoDto.getId().longValue()){
                    throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "该分组下设置的有效订单数范围存在重复");
                }
            }
        }else{ //新版数据
            if(nuserRewardRuleInfoDto.getNuserModel().intValue() == 104){//按最后下单时间算
                if(nuserRewardRuleInfoDto.getLastOrderDay() == null){
                    throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入最后下单时间大于天数");
                }

                //验证最后下单时间是否重复
                NuserRewardRule nuserRewardRule_lastOrderDay = nuserRewardRuleMapper.selectByLastOrderDay(nuserRewardRuleInfoDto);
                if(nuserRewardRule_lastOrderDay != null && nuserRewardRule_lastOrderDay.getId().longValue() != nuserRewardRuleInfoDto.getId().longValue()){
                    throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "该分组下设置的最后下单时间存在重复");
                }
            }else if(nuserRewardRuleInfoDto.getNuserModel().intValue() == 105){//按注册时间算
                if(nuserRewardRuleInfoDto.getRegDay() == null){
                    throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入注册时间小于天数");
                }
                //设置注册时间秒数
                nuserRewardRuleInfoDto.setRegTime(Tools.getInteger(nuserRewardRuleInfoDto.getRegDay() * 24 *3600));
                //验证佣金范围是否与数据中存在交叉
                NuserRewardRule nuserRewardRule_lastOrderDay = nuserRewardRuleMapper.selectByRegTime(nuserRewardRuleInfoDto);
                if(nuserRewardRule_lastOrderDay != null && nuserRewardRule_lastOrderDay.getId().intValue() != nuserRewardRuleInfoDto.getId().longValue()){
                    throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "该分组下设置的注册时间小于天数存在重复");
                }
            }else if(nuserRewardRuleInfoDto.getNuserModel().intValue() == 100 || nuserRewardRuleInfoDto.getNuserModel().intValue() == 101){ //全平台
                //全平台订单数
                if(nuserRewardRuleInfoDto.getAllMinOrderCount() == null){
                    throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入全平台订单大于等于数");
                }
                if(nuserRewardRuleInfoDto.getAllMaxOrderCount() == null){
                    throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入全平台订单小于数");
                }
                //最大佣金必须大于等于最小佣金
                if(nuserRewardRuleInfoDto.getAllMaxOrderCount().intValue() <= nuserRewardRuleInfoDto.getAllMinOrderCount().intValue()){
                    throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "最大订单数必须超过最小订单数");
                }
                //验证佣金范围是否与数据中存在交叉
                NuserRewardRule nuserRewardRule_AllOrderCount = nuserRewardRuleMapper.selectByAllOrderCount(nuserRewardRuleInfoDto);
                if(nuserRewardRule_AllOrderCount != null && nuserRewardRule_AllOrderCount.getId().longValue() != nuserRewardRuleInfoDto.getId().longValue()){
                    throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "该分组下设置的全平台订单数范围存在重复");
                }
            }else if(nuserRewardRuleInfoDto.getNuserModel().intValue() == 102 || nuserRewardRuleInfoDto.getNuserModel().intValue() == 103){
                //淘宝订单数
                if(nuserRewardRuleInfoDto.getTbMinOrderCount() != null && nuserRewardRuleInfoDto.getTbMaxOrderCount() != null){
                    //最大佣金必须大于等于最小佣金
                    if(nuserRewardRuleInfoDto.getTbMaxOrderCount().intValue() <= nuserRewardRuleInfoDto.getTbMinOrderCount().intValue()){
                        throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "淘宝最大订单数必须超过最小订单数");
                    }
                    //验证佣金范围是否与数据中存在交叉
                    NuserRewardRule nuserRewardRule_TbOrderCount = nuserRewardRuleMapper.selectByTbOrderCount(nuserRewardRuleInfoDto);
                    if(nuserRewardRule_TbOrderCount != null && nuserRewardRule_TbOrderCount.getId().longValue() != nuserRewardRuleInfoDto.getId().longValue()){
                        throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "该分组下设置的淘宝订单数范围存在重复");
                    }
                }
                //京东订单数
                if(nuserRewardRuleInfoDto.getJdMinOrderCount() != null && nuserRewardRuleInfoDto.getJdMaxOrderCount() != null){
                    //最大佣金必须大于等于最小佣金
                    if(nuserRewardRuleInfoDto.getJdMaxOrderCount().intValue() <= nuserRewardRuleInfoDto.getJdMinOrderCount().intValue()){
                        throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "京东最大订单数必须超过最小订单数");
                    }
                    //验证佣金范围是否与数据中存在交叉
                    NuserRewardRule nuserRewardRule_JdOrderCount = nuserRewardRuleMapper.selectByJdOrderCount(nuserRewardRuleInfoDto);
                    if(nuserRewardRule_JdOrderCount != null && nuserRewardRule_JdOrderCount.getId().longValue() != nuserRewardRuleInfoDto.getId().longValue()){
                        throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "该分组下设置的京东订单数范围存在重复");
                    }
                }

                //拼多多订单数
                if(nuserRewardRuleInfoDto.getPddMinOrderCount()  != null && nuserRewardRuleInfoDto.getPddMaxOrderCount() != null){
                    //最大佣金必须大于等于最小佣金
                    if(nuserRewardRuleInfoDto.getPddMaxOrderCount().intValue() <= nuserRewardRuleInfoDto.getPddMinOrderCount().intValue()){
                        throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "拼多多最大订单数必须超过最小订单数");
                    }
                    //验证佣金范围是否与数据中存在交叉
                    NuserRewardRule nuserRewardRule_PddOrderCount = nuserRewardRuleMapper.selectByPddOrderCount(nuserRewardRuleInfoDto);
                    if(nuserRewardRule_PddOrderCount != null && nuserRewardRule_PddOrderCount.getId().longValue() != nuserRewardRuleInfoDto.getId().longValue()){
                        throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "该分组下设置的拼多多订单数范围存在重复");
                    }
                }

                //唯品会订单数
                if(nuserRewardRuleInfoDto.getVphMinOrderCount() != null && nuserRewardRuleInfoDto.getVphMaxOrderCount() != null){
                    //最大佣金必须大于等于最小佣金
                    if(nuserRewardRuleInfoDto.getVphMaxOrderCount().intValue() <= nuserRewardRuleInfoDto.getVphMinOrderCount().intValue()){
                        throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "唯品会最大订单数必须超过最小订单数");
                    }
                    //验证佣金范围是否与数据中存在交叉
                    NuserRewardRule nuserRewardRule_VphOrderCount = nuserRewardRuleMapper.selectByVphOrderCount(nuserRewardRuleInfoDto);
                    if(nuserRewardRule_VphOrderCount != null && nuserRewardRule_VphOrderCount.getId().longValue() != nuserRewardRuleInfoDto.getId().longValue()){
                        throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "该分组下设置的唯品会订单数范围存在重复");
                    }
                }

                //抖音订单数
                if(nuserRewardRuleInfoDto.getDyMinOrderCount() != null && nuserRewardRuleInfoDto.getDyMaxOrderCount() == null){
                    //最大佣金必须大于等于最小佣金
                    if(nuserRewardRuleInfoDto.getDyMaxOrderCount().intValue() <= nuserRewardRuleInfoDto.getDyMinOrderCount().intValue()){
                        throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "抖音最大订单数必须超过最小订单数");
                    }
                    //验证佣金范围是否与数据中存在交叉
                    NuserRewardRule nuserRewardRule_DyOrderCount = nuserRewardRuleMapper.selectByDyOrderCount(nuserRewardRuleInfoDto);
                    if(nuserRewardRule_DyOrderCount != null && nuserRewardRule_DyOrderCount.getId().longValue() != nuserRewardRuleInfoDto.getId().longValue()){
                        throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "该分组下设置的抖音订单数范围存在重复");
                    }
                }

                //快手订单数
                if(nuserRewardRuleInfoDto.getKsMinOrderCount() != null && nuserRewardRuleInfoDto.getKsMaxOrderCount() != null){
                    //最大佣金必须大于等于最小佣金
                    if(nuserRewardRuleInfoDto.getKsMaxOrderCount().intValue() <= nuserRewardRuleInfoDto.getKsMinOrderCount().intValue()){
                        throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "快手最大订单数必须超过最小订单数");
                    }
                    //验证佣金范围是否与数据中存在交叉
                    NuserRewardRule nuserRewardRule_KsOrderCount = nuserRewardRuleMapper.selectByKsOrderCount(nuserRewardRuleInfoDto);
                    if(nuserRewardRule_KsOrderCount != null && nuserRewardRule_KsOrderCount.getId().longValue() != nuserRewardRuleInfoDto.getId().longValue()){
                        throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "该分组下设置的快手订单数范围存在重复");
                    }
                }

                //本地生活订单数
                if(nuserRewardRuleInfoDto.getLocalliveMinOrderCount() != null && nuserRewardRuleInfoDto.getLocalliveMaxOrderCount() != null){
                    //最大佣金必须大于等于最小佣金
                    if(nuserRewardRuleInfoDto.getLocalliveMaxOrderCount().intValue() <= nuserRewardRuleInfoDto.getLocalliveMinOrderCount().intValue()){
                        throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "本地生活最大订单数必须超过最小订单数");
                    }
                    //验证佣金范围是否与数据中存在交叉
                    NuserRewardRule nuserRewardRule_LocalliveOrderCount = nuserRewardRuleMapper.selectByLocalliveOrderCount(nuserRewardRuleInfoDto);
                    if(nuserRewardRule_LocalliveOrderCount != null && nuserRewardRule_LocalliveOrderCount.getId().longValue() != nuserRewardRuleInfoDto.getId().longValue()){
                        throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "该分组下设置的本地生活订单数范围存在重复");
                    }
                }
            }
        }

        if(nuserRewardRuleInfoDto.getRewardType() == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请分则分佣方式");
        }

        //总佣金分佣
        if(nuserRewardRuleInfoDto.getRewardType().intValue() == 0){
            if(nuserRewardRuleInfoDto.getUserRatio() == null){
                throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入买家自购比例");
            }
            if(nuserRewardRuleInfoDto.getParentRatio() == null){
                throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入给上级返利比例");
            }
            if(nuserRewardRuleInfoDto.getSlParentRatio() == null){
                throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入给上上级返利比例");
            }
            if(nuserRewardRuleInfoDto.getAgentRatio() == null){
                throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入给代理返利比例");
            }
            if(nuserRewardRuleInfoDto.getSlAgentRatio() == null){
                throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入给上级代理返利比例");
            }
            if(nuserRewardRuleInfoDto.getPartnerRatio() == null){
                throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入给合伙人返利比例");
            }
        }else{ //固定金额奖励
            if(nuserRewardRuleInfoDto.getReUserBal() == null){
                throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入买家奖励金额");
            }
            if(nuserRewardRuleInfoDto.getReParentBal() == null){
                throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入给上级奖励金额");
            }
            if(nuserRewardRuleInfoDto.getReSlParentBal() == null){
                throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入给上上级奖励金额");
            }
            if(nuserRewardRuleInfoDto.getReAgentBal() == null){
                throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入给代理奖励金额");
            }
            if(nuserRewardRuleInfoDto.getReSlAgentBal() == null){
                throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入给上级代理奖励金额");
            }
            if(nuserRewardRuleInfoDto.getRePartnerBal() == null){
                throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入给合伙人奖励金额");
            }
        }

        NuserRewardRule nuserRewardRule = nuserRewardRuleMapper.selectById(nuserRewardRuleInfoDto.getId(), nuserRewardRuleInfoDto.getMerId());
        if(nuserRewardRule == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "该杀熟机制不存在");
        }

        if(nuserRewardRule != null){
            nuserRewardRule.setGroupId(nuserRewardRuleInfoDto.getGroupId());
            nuserRewardRule.setNuserModel(nuserRewardRuleInfoDto.getNuserModel());
            nuserRewardRule.setLastOrderDay(nuserRewardRuleInfoDto.getLastOrderDay());
            nuserRewardRule.setLastOrderTime(Tools.getInteger(nuserRewardRuleInfoDto.getLastOrderDay()) * 24 * 3600);
            nuserRewardRule.setRegTime(Tools.getInteger(nuserRewardRuleInfoDto.getRegTime()));
            nuserRewardRule.setMinOrderCount(nuserRewardRuleInfoDto.getMinOrderCount());
            nuserRewardRule.setMaxOrderCount(nuserRewardRuleInfoDto.getMaxOrderCount());

            nuserRewardRule.setAllMinOrderCount(nuserRewardRuleInfoDto.getAllMinOrderCount());
            nuserRewardRule.setAllMaxOrderCount(nuserRewardRuleInfoDto.getAllMaxOrderCount());
            nuserRewardRule.setTbMinOrderCount(nuserRewardRuleInfoDto.getTbMinOrderCount());
            nuserRewardRule.setTbMaxOrderCount(nuserRewardRuleInfoDto.getTbMaxOrderCount());
            nuserRewardRule.setJdMinOrderCount(nuserRewardRuleInfoDto.getJdMinOrderCount());
            nuserRewardRule.setJdMaxOrderCount(nuserRewardRuleInfoDto.getJdMaxOrderCount());
            nuserRewardRule.setPddMinOrderCount(nuserRewardRuleInfoDto.getPddMinOrderCount());
            nuserRewardRule.setPddMaxOrderCount(nuserRewardRuleInfoDto.getPddMaxOrderCount());
            nuserRewardRule.setVphMinOrderCount(nuserRewardRuleInfoDto.getVphMinOrderCount());
            nuserRewardRule.setVphMaxOrderCount(nuserRewardRuleInfoDto.getVphMaxOrderCount());
            nuserRewardRule.setDyMinOrderCount(nuserRewardRuleInfoDto.getDyMinOrderCount());
            nuserRewardRule.setDyMaxOrderCount(nuserRewardRuleInfoDto.getDyMaxOrderCount());
            nuserRewardRule.setKsMinOrderCount(nuserRewardRuleInfoDto.getKsMinOrderCount());
            nuserRewardRule.setKsMaxOrderCount(nuserRewardRuleInfoDto.getKsMaxOrderCount());
            nuserRewardRule.setLocalliveMinOrderCount(nuserRewardRuleInfoDto.getLocalliveMinOrderCount());
            nuserRewardRule.setLocalliveMaxOrderCount(nuserRewardRuleInfoDto.getLocalliveMaxOrderCount());

            nuserRewardRule.setLadderRatioGroupId(nuserRewardRuleInfoDto.getLadderRatioGroupId());
            nuserRewardRule.setRewardType(nuserRewardRuleInfoDto.getRewardType());
            nuserRewardRule.setRewardType(nuserRewardRuleInfoDto.getRewardType());
            nuserRewardRule.setUserRatio(nuserRewardRuleInfoDto.getUserRatio());
            nuserRewardRule.setParentRatio(nuserRewardRuleInfoDto.getParentRatio());
            nuserRewardRule.setSlParentRatio(nuserRewardRuleInfoDto.getSlParentRatio());
            nuserRewardRule.setAgentRatio(nuserRewardRuleInfoDto.getAgentRatio());
            nuserRewardRule.setSlAgentRatio(nuserRewardRuleInfoDto.getSlAgentRatio());
            nuserRewardRule.setPartnerRatio(nuserRewardRuleInfoDto.getPartnerRatio());
            nuserRewardRule.setReUserBal(nuserRewardRuleInfoDto.getReUserBal());
            nuserRewardRule.setReParentBal(nuserRewardRuleInfoDto.getReParentBal());
            nuserRewardRule.setReSlParentBal(nuserRewardRuleInfoDto.getReSlParentBal());
            nuserRewardRule.setReAgentBal(nuserRewardRuleInfoDto.getReAgentBal());
            nuserRewardRule.setReSlAgentBal(nuserRewardRuleInfoDto.getReSlAgentBal());
            nuserRewardRule.setRePartnerBal(nuserRewardRuleInfoDto.getRePartnerBal());
            nuserRewardRule.setUpdateTime(DateUtil.getNowTime());
            nuserRewardRuleMapper.update(nuserRewardRule);
        }
    }

    /**
     * 删除杀熟规则
     * @author liuqi
     * @date 2021/8/25 11:38
     * @param nuserRewardRuleIdDto
     * @return null
     */
    public void deleteNuserRewardRule(NuserRewardRuleIdDto nuserRewardRuleIdDto){

        if(nuserRewardRuleIdDto.getId() == null ){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请选择杀熟规则id");
        }

        NuserRewardRule nuserRewardRule = nuserRewardRuleMapper.selectById(nuserRewardRuleIdDto.getId(), nuserRewardRuleIdDto.getMerId());

        if(nuserRewardRule == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请选择杀熟规则不存在");
        }

        NuserRewardRuleQuery nuserRewardRuleQuery =  new NuserRewardRuleQuery();
        nuserRewardRuleQuery.setMerId(nuserRewardRule.getMerId());
        nuserRewardRuleQuery.setGroupId(nuserRewardRule.getGroupId());
        List<NuserRewardRule> nuserRewardRuleList = nuserRewardRuleMapper.findByList(nuserRewardRuleQuery);
        if(nuserRewardRuleList != null && nuserRewardRuleList.size() ==1){ //最后一个规则，需要判断是否在使用
            //判断该杀熟组是否在分佣规则中使用
            NormalRewardRuleQuery normalRewardRuleQuery = new NormalRewardRuleQuery();
            normalRewardRuleQuery.setMerId(nuserRewardRuleIdDto.getMerId());
            normalRewardRuleQuery.setNuserRuleGroupId(nuserRewardRule.getGroupId());
            List<NormalRewardRule> normalRewardRuleList = normalRewardRuleMapper.findByList(normalRewardRuleQuery);
            if(normalRewardRuleList != null && normalRewardRuleList.size() > 0){
                throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "该杀熟规则组正在使用中，至少保留一个杀熟条件");
            }
        }
        //删除
        nuserRewardRuleMapper.deleteById(nuserRewardRuleIdDto.getId(), nuserRewardRuleIdDto.getMerId());
    }

    /**
     * 分页查询杀熟规则分组列表
     * @author liuqi
     * @date 2021/8/25 11:38
     * @param nuserRewardRuleGroupQuery
     * @return null
     */
    public Page<NuserRewardRuleGroupInfoVo> findNuserRewardRuleGroupList(NuserRewardRuleGroupQuery nuserRewardRuleGroupQuery){
        //新建返回到页面的LIST对象
        List<NuserRewardRuleGroupInfoVo> nuserRewardRuleGroupInfoVoList = new ArrayList<>();

        //执行分页查询对象
        Page<NuserRewardRuleGroup> page = new Page<>();
        nuserRewardRuleGroupQuery.convert(page);
        List<NuserRewardRuleGroup> nuserRewardRuleGroupList = nuserRewardRuleGroupMapper.findByList(page, nuserRewardRuleGroupQuery);

        if(nuserRewardRuleGroupList != null){
            for (NuserRewardRuleGroup nuserRewardRuleGroup : nuserRewardRuleGroupList){
                NuserRewardRuleGroupInfoVo nuserRewardRuleGroupInfoVo = new NuserRewardRuleGroupInfoVo();
                BeanUtils.copyProperties(nuserRewardRuleGroup, nuserRewardRuleGroupInfoVo);

                //杀熟规则列表
                try {
                    if(StringUtils.isNotBlank(nuserRewardRuleGroup.getRuleInfo())){
                        List<Integer> ruleInfoList = JSONObject.parseArray(nuserRewardRuleGroup.getRuleInfo(), Integer.class);
                        nuserRewardRuleGroupInfoVo.setRuleInfoList(ruleInfoList);
                    }
                }catch (Exception e){

                }

                nuserRewardRuleGroupInfoVoList.add(nuserRewardRuleGroupInfoVo);
            }
        }

        //返回到页面分页对象
        Page<NuserRewardRuleGroupInfoVo> pageVo = new Page<>();
        BeanUtils.copyProperties(page, pageVo);
        pageVo.setRecords(nuserRewardRuleGroupInfoVoList);
        //将分页信息复制到返回到页面的分页MO当中
        return pageVo;
    }

    /**
     * 添加杀熟规则分组
     * @author liuqi
     * @date 2021/8/25 11:38
     * @param nuserRewardRuleGroupInfoDto
     * @return null
     */
    public void addNuserRewardRuleGroup(NuserRewardRuleGroupInfoDto nuserRewardRuleGroupInfoDto){
        if(nuserRewardRuleGroupInfoDto.getRuleInfoList() == null || nuserRewardRuleGroupInfoDto.getRuleInfoList().size() == 0){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请选择杀熟规则");
        }

        if(StringUtils.isBlank(nuserRewardRuleGroupInfoDto.getName())){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入分组名称");
        }

        //验证是否重名
        NuserRewardRuleGroup nuserRewardRuleGroup_name = nuserRewardRuleGroupMapper.selectByName(nuserRewardRuleGroupInfoDto.getMerId(), nuserRewardRuleGroupInfoDto.getName());
        if(nuserRewardRuleGroup_name != null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "该组名称已经存在");
        }

        Integer now = DateUtil.getNowTime();
        NuserRewardRuleGroup nuserRewardRuleGroup = new NuserRewardRuleGroup();
        nuserRewardRuleGroup.setId(IdWorker.getId());
        nuserRewardRuleGroup.setMerId(nuserRewardRuleGroupInfoDto.getMerId());
        nuserRewardRuleGroup.setName(nuserRewardRuleGroupInfoDto.getName());
        nuserRewardRuleGroup.setRuleInfo(JSONObject.toJSONString(nuserRewardRuleGroupInfoDto.getRuleInfoList()));
        nuserRewardRuleGroup.setCreateTime(now);
        nuserRewardRuleGroup.setUpdateTime(now);
        nuserRewardRuleGroupMapper.insert(nuserRewardRuleGroup);
    }

    /**
     * 删除杀熟规则分组
     * @author liuqi
     * @date 2021/8/25 11:38
     * @param nuserRewardRuleGroupIdDto
     * @return null
     */
    public void deleteNuserRewardRuleGroup(NuserRewardRuleGroupIdDto nuserRewardRuleGroupIdDto){
        if(nuserRewardRuleGroupIdDto.getId() == null ){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请选择id");
        }

        //查询该分组是否已经被杀熟规则选用
        NuserRewardRuleQuery nuserRewardRuleQuery = new NuserRewardRuleQuery();
        nuserRewardRuleQuery.setGroupId(nuserRewardRuleGroupIdDto.getId());
        List<NuserRewardRule> nuserRewardRuleList = nuserRewardRuleMapper.findByList(nuserRewardRuleQuery);
        if(nuserRewardRuleList != null && nuserRewardRuleList.size() > 0){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "该分组已经被使用，不能删除");
        }

        //判断该杀熟组是否在分佣规则中使用
        NormalRewardRuleQuery normalRewardRuleQuery = new NormalRewardRuleQuery();
        normalRewardRuleQuery.setMerId(nuserRewardRuleGroupIdDto.getMerId());
        normalRewardRuleQuery.setNuserRuleGroupId(nuserRewardRuleGroupIdDto.getId());
        List<NormalRewardRule> normalRewardRuleList = normalRewardRuleMapper.findByList(normalRewardRuleQuery);
        if(normalRewardRuleList != null && normalRewardRuleList.size() > 0){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "此杀熟规则已经在分佣规则中使用，不能删除");
        }

        //查询此杀熟规则组是否在插件中使用，如果有就不能删除
//        SysPluginQuery sysPluginQuery = new SysPluginQuery();
//        sysPluginQuery.setData(String.valueOf(nuserRewardRuleGroupIdDto.getId()));
//        List<SysPluginVo> sysPluginVoList = sysPluginMapper.findByList(sysPluginQuery);
//        //判断插件是否在使用该杀熟机制
//        if(sysPluginVoList != null && sysPluginVoList.size() > 0){
//            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "此杀熟规则已经在插件管理中使用，不能删除");
//        }

        nuserRewardRuleGroupMapper.deleteById(nuserRewardRuleGroupIdDto.getId(), nuserRewardRuleGroupIdDto.getMerId());
    }

    /**
     * 修改杀熟规则分组
     * @author liuqi
     * @date 2021/8/25 11:38
     * @param nuserRewardRuleGroupInfoDto
     * @return null
     */
    public void updateNuserRewardRuleGroup(NuserRewardRuleGroupInfoDto nuserRewardRuleGroupInfoDto){

        if(nuserRewardRuleGroupInfoDto.getId() == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入id参数");
        }

        if(nuserRewardRuleGroupInfoDto.getRuleInfoList() == null || nuserRewardRuleGroupInfoDto.getRuleInfoList().size() == 0){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请选择杀熟规则");
        }

        if(StringUtils.isBlank(nuserRewardRuleGroupInfoDto.getName())){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入分组名称");
        }

        NuserRewardRuleGroup nuserRewardRuleGroup = nuserRewardRuleGroupMapper.selectById(nuserRewardRuleGroupInfoDto.getId(), nuserRewardRuleGroupInfoDto.getMerId());
        if(nuserRewardRuleGroup == null ){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "该类型分组不存在");
        }

        //验证新名字是否已经存在
        if(!nuserRewardRuleGroupInfoDto.getName().equals(nuserRewardRuleGroup.getName())){ //表示修改了组名称
            //验证新名称是否存在
            NuserRewardRuleGroup nuserRewardRuleGroup_name = nuserRewardRuleGroupMapper.selectByName(nuserRewardRuleGroupInfoDto.getMerId(), nuserRewardRuleGroupInfoDto.getName());
            if(nuserRewardRuleGroup_name != null){
                throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "该组名称已经存在");
            }
        }

        Integer now = DateUtil.getNowTime();
        nuserRewardRuleGroup.setName(nuserRewardRuleGroupInfoDto.getName());
        nuserRewardRuleGroup.setRuleInfo(JSONObject.toJSONString(nuserRewardRuleGroupInfoDto.getRuleInfoList()));
        nuserRewardRuleGroup.setUpdateTime(now);
        nuserRewardRuleGroupMapper.update(nuserRewardRuleGroup);
    }

}
